package light.head.dao.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.ReturnInfo;

import org.springframework.stereotype.Service;
/***
 * 物品归还信息
 * @author yy
 *
 */
@Service
public class ReturnInfoDAO extends BaseDAO<ReturnInfo, Integer>{
	
	
	/**
	 * 根据oc 获取领用信息
	 * @param oc	机构编号
	 * @param pb	分页信息
	 * @param map	查询条件
	 */
	public RESULT findByMap(List<Integer> ocs, PageBean pageBean, Map<String,String> map) {
		if (null == ocs || ocs.size() == 0) return RESULT.ERROR;
		StringBuffer hql = new StringBuffer(" FROM ReturnInfo ret WHERE ret.deleted = 0 AND ret.oc IN (");
		List<Object> list = new ArrayList<Object>();
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		if (map != null) {
			String cn = map.get(Parm.CN);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String deleted = map.get(Parm.DELETED);
			String affirm = map.get(Parm.AFFIRM);
			
			
			if (isNotNull(affirm)) {
				list.add(Boolean.parseBoolean(affirm));
				hql.append(" AND ret.affirm = ?");
			}
			if(isNotNull(deleted)){
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND ret.deleted = ?");
			}if(isNotNull(cn)){
				list.add("%" + cn + "%");
				hql.append(" AND ret.cn LIKE ?");
			}if(isNotNull(time1)){
				list.add(DateUtils.getDateByStr(time1, null));
				hql.append("AND ret.ct >= ?");
			}if(isNotNull(time2)){
				list.add(DateUtils.getDateByStr(time2, null));
				hql.append("AND ret.ct <= ?");
			}
		}
		hql.append(" ORDER BY ret.code DESC");
		return findToPageBean(hql.toString(), pageBean, list);
	}
	
	
	/***
	 * 根据cc 获取归还信息
	 * @param cc  归还人
	 * @param pageBean
	 * @param map
	 * @return
	 */
	public RESULT findByCu(Integer cu,PageBean pageBean, Map<String,String> map){
		if(null == cu) return RESULT.ERROR;
		StringBuffer hql = new StringBuffer("From ReturnInfo ret WHERE ret.deleted = 0");
		List<Object> list = new ArrayList<Object>();
		hql.append(" AND ret.cu = ?");
		list.add(cu);
		if(map != null){
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String deleted = map.get(Parm.DELETED);
			String affirm = map.get(Parm.AFFIRM);
			String cname = map.get(Parm.CNAME);
			
			if (isNotNull(affirm)) {
				list.add(Boolean.parseBoolean(affirm));
				hql.append(" AND ret.affirm = ?");
			}if (isNotNull(deleted)) {
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND ret.deleted = ?");
			}if (isNotNull(time1)) {
				list.add(DateUtils.getDateByStr(time1, null));
				hql.append(" AND ret.ct >= ?");
			}if(isNotNull(time2)){
				list.add(DateUtils.getDateByStr(time2, null));
				hql.append("AND ret.ct <= ?");
			}if(isNotNull(cname)){
				list.add("%" + cname + "%");
				hql.append(" AND ret.cname LIKE ?");
			}
		}
		hql.append(" ORDER BY ret.code DESC");
		return findToPageBean(hql.toString(),pageBean, list);
	}
}
